<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>出现回到顶部的按钮</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    :root {
      background-color: pink;
    }

    body {
      width: 100%;
    }

    #home {
      width: 100px;
      height: 100px;
      background-color: green;
      position: fixed;
      right: 20px;
      top: 80vh;
      z-index: 1000;
      border-radius: 50%;
    }

    .cls1 {
      width: 100%;
      height: 50vh;
      background-color: hotpink;
    }

    .cls2 {
      width: 100%;
      height: 300px;
      background-color: indianred;
    }

    .cls3 {
      width: 100%;
      height: 100vh;
      background-color: lightblue;
    }
  </style>
</head>

<body>
  <div class="cls1"></div>
  <div class="cls2"></div>
  <div class="cls3"></div>
  <div id="home"></div>
</body>
<script>
  "use strict";
  const observer = new IntersectionObserver(entires => {
    //entires为要监听的元素
    entires.forEach((value, key, arr) => {
      console.log(value);
      console.log(value.target.offsetTop);
      console.log(value.target.scrollTop);

      console.log(value.rootBounds);
      console.log(value.intersectionRect);
      console.log(value.boundingClientRect);
    });
  });
  let i = document.getElementById("home");
  observer.observe(i);
</script>

</html>